home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / js / jsarena.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  14KB  |  312 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. #ifndef jsarena_h___
  41. #define jsarena_h___
  42. /*
  43.  * Lifetime-based fast allocation, inspired by much prior art, including
  44.  * "Fast Allocation and Deallocation of Memory Based on Object Lifetimes"
  45.  * David R. Hanson, Software -- Practice and Experience, Vol. 20(1).
  46.  *
  47.  * Also supports LIFO allocation (JS_ARENA_MARK/JS_ARENA_RELEASE).
  48.  */
  49. #include <stdlib.h>
  50. #include "jstypes.h"
  51. #include "jscompat.h"
  52.  
  53. JS_BEGIN_EXTERN_C
  54.  
  55. typedef struct JSArena JSArena;
  56. typedef struct JSArenaPool JSArenaPool;
  57.  
  58. struct JSArena {
  59.     JSArena     *next;          /* next arena for this lifetime */
  60.     jsuword     base;           /* aligned base address, follows this header */
  61.     jsuword     limit;          /* one beyond last byte in arena */
  62.     jsuword     avail;          /* points to next available byte */
  63. };
  64.  
  65. #ifdef JS_ARENAMETER
  66. typedef struct JSArenaStats JSArenaStats;
  67.  
  68. struct JSArenaStats {
  69.     JSArenaStats *next;         /* next in arenaStats list */
  70.     char        *name;          /* name for debugging */
  71.     uint32      narenas;        /* number of arenas in pool */
  72.     uint32      nallocs;        /* number of JS_ARENA_ALLOCATE() calls */
  73.     uint32      nreclaims;      /* number of reclaims from freeArenas */
  74.     uint32      nmallocs;       /* number of malloc() calls */
  75.     uint32      ndeallocs;      /* number of lifetime deallocations */
  76.     uint32      ngrows;         /* number of JS_ARENA_GROW() calls */
  77.     uint32      ninplace;       /* number of in-place growths */
  78.     uint32      nreallocs;      /* number of arena grow extending reallocs */
  79.     uint32      nreleases;      /* number of JS_ARENA_RELEASE() calls */
  80.     uint32      nfastrels;      /* number of "fast path" releases */
  81.     size_t      nbytes;         /* total bytes allocated */
  82.     size_t      maxalloc;       /* maximum allocation size in bytes */
  83.     double      variance;       /* size variance accumulator */
  84. };
  85. #endif
  86.  
  87. struct JSArenaPool {
  88.     JSArena     first;          /* first arena in pool list */
  89.     JSArena     *current;       /* arena from which to allocate space */
  90.     size_t      arenasize;      /* net exact size of a new arena */
  91.     jsuword     mask;           /* alignment mask (power-of-2 - 1) */
  92. #ifdef JS_ARENAMETER
  93.     JSArenaStats stats;
  94. #endif
  95. };
  96.  
  97. /*
  98.  * If the including .c file uses only one power-of-2 alignment, it may define
  99.  * JS_ARENA_CONST_ALIGN_MASK to the alignment mask and save a few instructions
  100.  * per ALLOCATE and GROW.
  101.  */
  102. #ifdef JS_ARENA_CONST_ALIGN_MASK
  103. #define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + JS_ARENA_CONST_ALIGN_MASK)   \
  104.                                  & ~(jsuword)JS_ARENA_CONST_ALIGN_MASK)
  105.  
  106. #define JS_INIT_ARENA_POOL(pool, name, size) \
  107.         JS_InitArenaPool(pool, name, size, JS_ARENA_CONST_ALIGN_MASK + 1)
  108. #else
  109. #define JS_ARENA_ALIGN(pool, n) (((jsuword)(n) + (pool)->mask) & ~(pool)->mask)
  110. #endif
  111.  
  112. #define JS_ARENA_ALLOCATE(p, pool, nb)                                        \
  113.     JS_ARENA_ALLOCATE_CAST(p, void *, pool, nb)
  114.  
  115. #define JS_ARENA_ALLOCATE_TYPE(p, type, pool)                                 \
  116.     JS_ARENA_ALLOCATE_CAST(p, type *, pool, sizeof(type))
  117.  
  118. /*
  119.  *
  120.  * NB: In JS_ARENA_ALLOCATE_CAST and JS_ARENA_GROW_CAST, always subtract _nb
  121.  * from a->limit rather than adding _nb to _p, to avoid overflowing a 32-bit
  122.  * address space (possible when running a 32-bit program on a 64-bit system
  123.  * where the kernel maps the heap up against the top of the 32-bit address
  124.  * space).
  125.  *
  126.  * Thanks to Juergen Kreileder <jk@blackdown.de>, who brought this up in
  127.  * https://bugzilla.mozilla.org/show_bug.cgi?id=279273.
  128.  */
  129. #define JS_ARENA_ALLOCATE_CAST(p, type, pool, nb)                             \
  130.     JS_BEGIN_MACRO                                                            \
  131.         JSArena *_a = (pool)->current;                                        \
  132.         size_t _nb = JS_ARENA_ALIGN(pool, nb);                                \
  133.         jsuword _p = _a->avail;                                               \
  134.         if (_p > _a->limit - _nb)                                             \
  135.             _p = (jsuword)JS_ArenaAllocate(pool, _nb);                        \
  136.         else                                                                  \
  137.             _a->avail = _p + _nb;                                             \
  138.         p = (type) _p;                                                        \
  139.         JS_ArenaCountAllocation(pool, nb);                                    \
  140.     JS_END_MACRO
  141.  
  142. #define JS_ARENA_GROW(p, pool, size, incr)                                    \
  143.     JS_ARENA_GROW_CAST(p, void *, pool, size, incr)
  144.  
  145. #define JS_ARENA_GROW_CAST(p, type, pool, size, incr)                         \
  146.     JS_BEGIN_MACRO                                                            \
  147.         JSArena *_a = (pool)->current;                                        \
  148.         if (_a->avail == (jsuword)(p) + JS_ARENA_ALIGN(pool, size)) {         \
  149.             size_t _nb = (size) + (incr);                                     \
  150.             _nb = JS_ARENA_ALIGN(pool, _nb);                                  \
  151.             if ((jsuword)(p) <= _a->limit - _nb) {                            \
  152.                 _a->avail = (jsuword)(p) + _nb;                               \
  153.                 JS_ArenaCountInplaceGrowth(pool, size, incr);                 \
  154.             } else if ((jsuword)(p) == _a->base) {                            \
  155.                 p = (type) JS_ArenaRealloc(pool, p, size, incr);              \
  156.             } else {                                                          \
  157.                 p = (type) JS_ArenaGrow(pool, p, size, incr);                 \
  158.             }                                                                 \
  159.         } else {                                                              \
  160.             p = (type) JS_ArenaGrow(pool, p, size, incr);                     \
  161.         }                                                                     \
  162.         JS_ArenaCountGrowth(pool, size, incr);                                \
  163.     JS_END_MACRO
  164.  
  165. #define JS_ARENA_MARK(pool)     ((void *) (pool)->current->avail)
  166. #define JS_UPTRDIFF(p,q)        ((jsuword)(p) - (jsuword)(q))
  167.  
  168. #ifdef DEBUG
  169. #define JS_FREE_PATTERN         0xDA
  170. #define JS_CLEAR_UNUSED(a)      (JS_ASSERT((a)->avail <= (a)->limit),         \
  171.                                  memset((void*)(a)->avail, JS_FREE_PATTERN,   \
  172.                                         (a)->limit - (a)->avail))
  173. #define JS_CLEAR_ARENA(a)       memset((void*)(a), JS_FREE_PATTERN,           \
  174.                                        (a)->limit - (jsuword)(a))
  175. #else
  176. #define JS_CLEAR_UNUSED(a)      /* nothing */
  177. #define JS_CLEAR_ARENA(a)       /* nothing */
  178. #endif
  179.  
  180. #define JS_ARENA_RELEASE(pool, mark)                                          \
  181.     JS_BEGIN_MACRO                                                            \
  182.         char *_m = (char *)(mark);                                            \
  183.         JSArena *_a = (pool)->current;                                        \
  184.         if (_a != &(pool)->first &&                                           \
  185.             JS_UPTRDIFF(_m, _a->base) <= JS_UPTRDIFF(_a->avail, _a->base)) {  \
  186.             _a->avail = (jsuword)JS_ARENA_ALIGN(pool, _m);                    \
  187.             JS_ASSERT(_a->avail <= _a->limit);                                \
  188.             JS_CLEAR_UNUSED(_a);                                              \
  189.             JS_ArenaCountRetract(pool, _m);                                   \
  190.         } else {                                                              \
  191.             JS_ArenaRelease(pool, _m);                                        \
  192.         }                                                                     \
  193.         JS_ArenaCountRelease(pool, _m);                                       \
  194.     JS_END_MACRO
  195.  
  196. #ifdef JS_ARENAMETER
  197. #define JS_COUNT_ARENA(pool,op) ((pool)->stats.narenas op)
  198. #else
  199. #define JS_COUNT_ARENA(pool,op)
  200. #endif
  201.  
  202. #define JS_ARENA_DESTROY(pool, a, pnext)                                      \
  203.     JS_BEGIN_MACRO                                                            \
  204.         JS_COUNT_ARENA(pool,--);                                              \
  205.         if ((pool)->current == (a)) (pool)->current = &(pool)->first;         \
  206.         *(pnext) = (a)->next;                                                 \
  207.         JS_CLEAR_ARENA(a);                                                    \
  208.         free(a);                                                              \
  209.         (a) = NULL;                                                           \
  210.     JS_END_MACRO
  211.  
  212. /*
  213.  * Initialize an arena pool with the given name for debugging and metering,
  214.  * with a minimum size per arena of size bytes.
  215.  */
  216. extern JS_PUBLIC_API(void)
  217. JS_InitArenaPool(JSArenaPool *pool, const char *name, size_t size,
  218.                  size_t align);
  219.  
  220. /*
  221.  * Free the arenas in pool.  The user may continue to allocate from pool
  222.  * after calling this function.  There is no need to call JS_InitArenaPool()
  223.  * again unless JS_FinishArenaPool(pool) has been called.
  224.  */
  225. extern JS_PUBLIC_API(void)
  226. JS_FreeArenaPool(JSArenaPool *pool);
  227.  
  228. /*
  229.  * Free the arenas in pool and finish using it altogether.
  230.  */
  231. extern JS_PUBLIC_API(void)
  232. JS_FinishArenaPool(JSArenaPool *pool);
  233.  
  234. /*
  235.  * Finish using arenas, freeing all memory associated with them except for
  236.  * any locks needed for thread safety.
  237.  */
  238. extern JS_PUBLIC_API(void)
  239. JS_ArenaFinish(void);
  240.  
  241. /*
  242.  * Free any locks or other memory needed for thread safety, just before
  243.  * shutting down.  At that point, we must be called by a single thread.
  244.  *
  245.  * After shutting down, the next thread to call JS_InitArenaPool must not
  246.  * race with any other thread.  Once a pool has been initialized, threads
  247.  * may safely call jsarena.c functions on thread-local pools.  The upshot
  248.  * is that pools are per-thread, but the underlying global freelist is
  249.  * thread-safe, provided that both the first pool initialization and the
  250.  * shut-down call are single-threaded.
  251.  */
  252. extern JS_PUBLIC_API(void)
  253. JS_ArenaShutDown(void);
  254.  
  255. /*
  256.  * Friend functions used by the JS_ARENA_*() macros.
  257.  */
  258. extern JS_PUBLIC_API(void *)
  259. JS_ArenaAllocate(JSArenaPool *pool, size_t nb);
  260.  
  261. extern JS_PUBLIC_API(void *)
  262. JS_ArenaRealloc(JSArenaPool *pool, void *p, size_t size, size_t incr);
  263.  
  264. extern JS_PUBLIC_API(void *)
  265. JS_ArenaGrow(JSArenaPool *pool, void *p, size_t size, size_t incr);
  266.  
  267. extern JS_PUBLIC_API(void)
  268. JS_ArenaRelease(JSArenaPool *pool, char *mark);
  269.  
  270. /*
  271.  * Function to be used directly when an allocation has likely grown to consume
  272.  * an entire JSArena, in which case the arena is returned to the malloc heap.
  273.  */
  274. extern JS_PUBLIC_API(void)
  275. JS_ArenaFreeAllocation(JSArenaPool *pool, void *p, size_t size);
  276.  
  277. #ifdef JS_ARENAMETER
  278.  
  279. #include <stdio.h>
  280.  
  281. extern JS_PUBLIC_API(void)
  282. JS_ArenaCountAllocation(JSArenaPool *pool, size_t nb);
  283.  
  284. extern JS_PUBLIC_API(void)
  285. JS_ArenaCountInplaceGrowth(JSArenaPool *pool, size_t size, size_t incr);
  286.  
  287. extern JS_PUBLIC_API(void)
  288. JS_ArenaCountGrowth(JSArenaPool *pool, size_t size, size_t incr);
  289.  
  290. extern JS_PUBLIC_API(void)
  291. JS_ArenaCountRelease(JSArenaPool *pool, char *mark);
  292.  
  293. extern JS_PUBLIC_API(void)
  294. JS_ArenaCountRetract(JSArenaPool *pool, char *mark);
  295.  
  296. extern JS_PUBLIC_API(void)
  297. JS_DumpArenaStats(FILE *fp);
  298.  
  299. #else  /* !JS_ARENAMETER */
  300.  
  301. #define JS_ArenaCountAllocation(ap, nb)                 /* nothing */
  302. #define JS_ArenaCountInplaceGrowth(ap, size, incr)      /* nothing */
  303. #define JS_ArenaCountGrowth(ap, size, incr)             /* nothing */
  304. #define JS_ArenaCountRelease(ap, mark)                  /* nothing */
  305. #define JS_ArenaCountRetract(ap, mark)                  /* nothing */
  306.  
  307. #endif /* !JS_ARENAMETER */
  308.  
  309. JS_END_EXTERN_C
  310.  
  311. #endif /* jsarena_h___ */
  312.